
#include "MCU_include.h"
vu16 u16_PWM_F_BUFF_2=0;

void PowerControl()
{
			
				if(FLAG.flagBit.PanMovOut == 0 && FG_IH_Status.flagBit.PWM_ON_OFF )	//There is a pot and PWM is turned on
				{
					if(1 == OVP4_Over_I_flag /*&& ( u16_PowerMea < u16_PowerSet_Target_H )*/ )	
					{
						POWER_CTRL.flagBit.Power_OK = 0;
					//	OVP4_Over_I_flag =0;
										
						u16_PWM_F_BUFF_2 = _pwmph;
						u16_PWM_F_BUFF_2 = ( (u16_PWM_F_BUFF_2 *256)  +( _pwmpl));
							PWM_F = PWM_SF/u16_PWM_F_BUFF_2;	//
	
					}
					FLAG.flagBit.PowerONFlag =1;
					if(POWER_CTRL.flagBit.Power_OK == 1)						
					{

						PowerMeaCnt++;					
						u16_PowerMeaSum+=u16_PowerMea;
						if(PowerMeaCnt >= 8)
						{
							PowerMeaCnt = 0;
							u16_PowerMea = u16_PowerMeaSum>>3;				
							u16_PowerMeaSum = 0;
							POWER_CTRL.flagBit.Power_OK = 0;
						}
						else
						POWER_CTRL.flagBit.Power_OK = 1;
					}
					if(POWER_CTRL.flagBit.Power_OK == 0)
					{
						u16_PowerMeaSum = 0;
						PowerMeaCnt = 0;
						if((u16_PowerMea < u16_PowerSet_Target_L) && OVP4_Over_I_flag==0 )							
						{

							if ((FLAG.flagBit.PanMovOutCheck == 0) && (PWM_F > MIN_FRE) )
							{
								Power_Add(MIN_FRE);		
								POWER_CTRL.flagBit.PowerAdd = 1;
								POWER_CTRL.flagBit.PowerCutdown = 0;
								PowerAddCnt++;
							}
						}
						else if(u16_PowerMea > u16_PowerSet_Target_H)						
						{
							POWER_CTRL.flagBit.Power_OK = 0;
	
							if ((FLAG.flagBit.PanMovOutCheck == 0) && (PWM_F < MAX_FRE))
							{
								Power_CutDown(MAX_FRE);	
								POWER_CTRL.flagBit.PowerAdd = 0;
								POWER_CTRL.flagBit.PowerCutdown = 1;
								POWER_CTRL.flagBit.FerMin = 0;
							}									
						}
						else
						{
							POWER_CTRL.flagBit.Power_OK = 1;	
							POWER_CTRL.flagBit.PowerAdd = 0;
							POWER_CTRL.flagBit.PowerCutdown = 0;
							FG_IH_Status.flagBit.PWOER_SET_CHANGE =0;
							
//								B_FLAG_PowerStable1 = 0;
//								B_FLAG_PowerStable2 = 0;
//								PowerStableCnt = 0;
//								Lim_Frequ = PWM_F - 200;
//								if (Lim_Frequ < MIN_FRE)
//								Lim_Frequ = MIN_FRE;							
//								PWMMAX_Update(Lim_Frequ);
							//	POWER_CTRL.flagBit.FreLimit = 1;
							OVP4_Over_I_flag =0;
											
						}
					}
				}
			
}				



void Power_Add(unsigned int MinFrequency)
{                        
	_clrwdt();
	vu16 POWER_Distance;
	POWER_Distance =	u16_PowerSet - u16_PowerMea  ;
	if(POWER_Distance >= POWER_DIFF_500)
		{	
			if(FG_IH_Status.flagBit.PWOER_SET_CHANGE)
			{			
				if(u16_PowerSet <=1600)
				{
					PWM_F = PWM_F - FRE_500;
				}
				else			
				{
					PWM_F = PWM_F - FRE_200;	
					
				}				
			}
			else
			{
				PWM_F = PWM_F - FRE_20;	
			
			}
		}
	else if(POWER_Distance >= POWER_DIFF_200)
		{		
			if(FG_IH_Status.flagBit.PWOER_SET_CHANGE)
			{	
				if(u16_PowerSet <=1600)
				{
					PWM_F = PWM_F - FRE_200;
				}
				else			
				{
					PWM_F = PWM_F - FRE_10;				
				}
			}
			else
			{
				PWM_F = PWM_F - FRE_10;	
			}			
		}
	else
		{	
			if(FG_IH_Status.flagBit.PWOER_SET_CHANGE)
			{	
				if(u16_PowerSet <=1600)
				{
					PWM_F = PWM_F - FRE_20;
				}
				else			
				{
					PWM_F = PWM_F - FRE_5;					
				}
			}
			else
			{
				PWM_F = PWM_F - FRE_5;	
			}				
		}	


	if (PWM_F < MinFrequency)
	PWM_F = MinFrequency;
	PWM_Update(PWM_F);		
}


void Power_CutDown(unsigned int MaxFrequency)
{                        
	_clrwdt();
	vu16 POWER_Distance;
 	POWER_Distance =	u16_PowerMea - u16_PowerSet  ;

		if(POWER_Distance >= POWER_DIFF_500)
		{
			if(u16_PowerSet <=1600)
			{
				PWM_F = PWM_F + FRE_500;
			}
			else			
			{
				PWM_F = PWM_F + FRE_200;	
				
			}			
		}
		else 	if(POWER_Distance >= POWER_DIFF_200)
		{
			if(u16_PowerSet <=1600)
			{
				PWM_F = PWM_F + FRE_200;
			}
			else			
			{
				PWM_F = PWM_F + FRE_10;	
				
			}
	
		}
		else 
		{
			if(u16_PowerSet <=1600)
			{
				PWM_F = PWM_F + FRE_20;
			}
			else			
			{
				PWM_F = PWM_F + FRE_5;	
				
			}
		
						
		}


	if (PWM_F > MaxFrequency)
	PWM_F = MaxFrequency;
	PWM_Update(PWM_F);		
}


void PowerOFF()
{
	if (_pwmon)
	{
		IGBT_OFF(PWM_F);
	}
	FG_IH_Status.flagBit.PWM_ON_OFF = 0;
	IH_Status_APP.m_data = 0;
//	FLAG.flagBit.PanMovOut == 0; 
	PWM_F = d_PWM_F;
	Var_Init();			
	FG_IH_Status.flagBit.Machine_ON_OFF = 0;
	IH_Status_APP.flagBit.Machine_ON_OFF =0;
	FLAG.flagBit.PowerONFlag =0;
 	FG_IH_Status.flagBit.PWOER_SET_CHANGE =1;
	FanDelay = 100;
	u16_PowerSet=0;
	BUZZER_ON_500MS();	
}